package com.controller.backend;

import com.biz.BillBiz;
import com.biz.CustomerBiz;
import com.core.BizException;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.Bill;
import com.entity.model.Customer;
import com.entity.vo.BillListVO;
import com.github.pagehelper.PageInfo;
import com.util.webpage.PageData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by jc on 2019/11/16.
 */
@Controller
@RequestMapping("/backend/security/bill")
public class BillController extends BaseController<Bill> {
    @Autowired
    private BillBiz billBiz;

    @Autowired
    private CustomerBiz customerBiz;

    //订单列表
    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/backend/security/bill/list";
    }

    //开通会员订单列表
    @RequestMapping(value = "vipIndex")
    public String vipIndex() throws Exception {
        return "/backend/security/bill/vipBillList";
    }

    //提现订单列表
    @RequestMapping(value = "commissionIndex")
    public String commissionIndex() throws Exception {
        return "/backend/security/bill/commissionList";
    }

    //订单详情
    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/backend/security/bill/form";
    }

    @RequestMapping(value = "bdStatistics")
    public String bdStatistics() throws Exception {
        return "/backend/security/bdStatistics/list";
    }

    @RequestMapping(value = "vipPerformance")
    public String vipPerformance() throws Exception {
        return "/backend/security/vipPerformance/list";
    }

    @RequestMapping(value = "statisticsDetail")
    public String statisticsDetail() throws Exception {
        return "/backend/security/bdStatistics/form";
    }

    //获取订单列表详情
    @RequestMapping(value = "billList")
    @ResponseBody
    public Result billList(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<Bill> list = billBiz.billList(cond);
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }

    //获取开通会员订单列表详情
    @RequestMapping(value = "vipBillList")
    @ResponseBody
    public Result vipBillList(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<Bill> list = billBiz.vipBillList(cond);
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }


    //获取佣金订单列表详情
    @RequestMapping(value = "commissionBillList")
    @ResponseBody
    public Result commissionBillList(HttpServletRequest request) {
        PageData cond = new PageData(request);

        Customer customer = getAccount(cond.getString("account"));
        if (customer != null) {
            cond.put("parentCustomerId", customer.getId());
        }
        List<Bill> list = billBiz.commissionBillList(cond);
        Bill commissionBill = billBiz.findCommissionBillListSum(cond);
        if (commissionBill != null && commissionBill.getProfitFee() != null && commissionBill.getProfitFee() > 0) {
            list.add(0, commissionBill);
        }
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }


    public Customer getAccount(String account) throws BizException {
        Condition cond = new Condition(Customer.class);
        cond.createCriteria().andEqualTo("account", account);
        List<Customer> list = customerBiz.findByCondition(cond);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    //获取佣金订单列表详情
    @RequestMapping(value = "commissionBillDetailList")
    @ResponseBody
    public Result commissionBillDetailList(Integer withdrawBillId, Integer customerId) {
        PageData cond = new PageData();
        cond.put("withdrawBillId", withdrawBillId);
        cond.put("parentCustomerId", customerId);
        List<Bill> list = billBiz.commissionBillList(cond);
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }


    @RequestMapping(value = "bdStatisticsList")
    @ResponseBody
    public Result bdStatisticsList(HttpServletRequest request) {
        PageData cond = new PageData(request);

        List<Bill> list = billBiz.bdStatisticsList(cond);
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }
}
